package com.spring.sunflower.dialog

import android.content.Context

import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.core.CenterPopupView
import com.spring.sunflower.R
import com.spring.sunflower.bean.GiveGiftBean
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.conversation.ChatSendGiftAdapter
import com.spring.sunflower.data.RequestDataSource
import com.spring.sunflower.event.ChatSendGiftEvent
import com.spring.sunflower.event.GiftSendSuccessEvent
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.observers.DisposableObserver
import io.reactivex.rxjava3.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by Kevin on 2021/8/6<br/>
 *
 * Describe:<br/>
 * 推送给男方，送女方礼物拉进距离弹窗
 */
class ChatSendGiftDialog(
    context: Context,
) : CenterPopupView(context) {
    private var tvTitle: TextView? = null
    private var tvOK: TextView? = null
    private var tvCancel: TextView? = null
    private var rvRecyclerView: RecyclerView? = null
    private var mAdapter: ChatSendGiftAdapter? = null
    private var mData: MutableList<ChatSendGiftEvent.GiftListBean> = ArrayList()
    private var clickPos = 0
    private var mGiftData = ChatSendGiftEvent()
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_chat_send_gift
    }

    override fun onCreate() {
        super.onCreate()
        tvTitle = findViewById(R.id.tvTitle)
        tvOK = findViewById(R.id.tvOK)
        tvCancel = findViewById(R.id.tvCancel)
        rvRecyclerView = findViewById(R.id.rvRecyclerView)
        val gridLayoutManager = GridLayoutManager(context, 3)
        rvRecyclerView?.layoutManager = gridLayoutManager
        mAdapter = ChatSendGiftAdapter(R.layout.adapter_item_chat_send_gift, mData)
        rvRecyclerView?.adapter = mAdapter
        listener?.onCrateView()
        tvCancel?.setOnClickListener { dismiss() }
        tvOK?.setOnClickListener {
//            listener?.onOKClick(clickPos)
            sendGift()
            dismiss()
        }
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            clickPos = position
            for (index in mData.indices) {
                mData[index].isChecked = index == position
            }
            mAdapter?.notifyDataSetChanged()
        }
    }

    private var listener: OnSendListener? = null
    fun setOnSendListener(l: OnSendListener) {
        listener = l
    }

    interface OnSendListener {
        fun onCrateView()
//        fun onOKClick(position: Int)
    }

    fun addData(data: ChatSendGiftEvent) {
        tvTitle?.text = if (data.nickname.isNullOrEmpty()) "TA" else data.nickname
        mGiftData = data
        val giftList = data.giftList
        if (giftList.size > 2) {
            clickPos = 1
        } else if (giftList.size in 1..2) {
            clickPos = 0
        }
        for (index in giftList.indices) {
            giftList[index].isChecked = index == clickPos
        }
        mAdapter?.addData(giftList)
    }

    fun sendGift() {
        val mUserID = SPUtils.getInstance().getString(Constant.KEY_USERID, "")
        val map: MutableMap<String, String> = HashMap()
        map["MID"] = mUserID
        map["anchorId"] = mGiftData.anchorId.toString()
        map["giftId"] = mData[clickPos].id.toString()
        map["number"] = "1"
        map["roomId"] = "0"
        map["type"] = "1"
        val instance = RequestDataSource.getInstance()
        val observable = instance.sendGift(map)
        observable.subscribeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : DisposableObserver<GiveGiftBean>() {
                override fun onNext(t: GiveGiftBean) {
                    when (t.status) {
                        "-1" -> {

                        }
                        "-2" -> {
                            DialogManager.getInstance().showNeedChargeDialog(context)
                        }
                        "0" -> {
                            ToastUtils.showShort("请求失败")
                        }
                        "1" -> {
                            SPUtils.getInstance().put(Constant.KEY_MONEY, t.datas.data)
                            val giftSendSuccessEvent = GiftSendSuccessEvent()
                            val d = mData[clickPos]
                            giftSendSuccessEvent.giftPath = d.imagePath
                            giftSendSuccessEvent.giftEffect = d.effects
                            giftSendSuccessEvent.giftName = d.name
                            giftSendSuccessEvent.giftNum = "1"
                            EventBus.getDefault().post(giftSendSuccessEvent)
                        }
                    }
                }

                override fun onError(e: Throwable?) {
                    ToastUtils.showShort(e?.message)
                }

                override fun onComplete() {
                    dismiss()
                }
            })

    }

}